﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace fes
{
  class ROC : Indicator, IIndicator  //http://www.marketvolume.com/technicalanalysis/roc.asp
  {
    private int n = 0;
    private List<double> closePrices;
    private double currentValue = -1;
    
    public ROC(Dictionary<String, Object> parameters) : base("ROC")
    {
      base.setInidicatorParams(parameters);
      n = (int) parameters["n"] + 1;
      closePrices = new List<double>();
    }

    public override void pushNextDayData(double open, double high, double low, double close, int vol)
    {
      closePrices.Add(close);
      if (closePrices.Count > n)
      {
        closePrices.RemoveAt(0);
      }
      calculateROC();
    }

    public override Indicator Clone()
    {
      Indicator r = new ROC(base.P);
      r.P = new Dictionary<string, object>();
      foreach (KeyValuePair<String, object> kvp in base.P)
        r.P.Add(kvp.Key, kvp.Value);
      foreach (FuzzySet fs in fuzzySets)
      {
        r.fuzzySets.Add(new FuzzySet(fs.Hedge, fs.name, fs.T));
      }
      return r;
    }
    
    public void setParameters(Dictionary<String, Object> parameters)
    {
        
        base.setInidicatorParams(parameters);
        n = (int)parameters["n"] + 1;
    }

    public override double getValue()
    {
      return currentValue;
    }

    private void calculateROC()
    {
      if (closePrices.Count == n)  
      {
        int cnt = closePrices.Count;
        currentValue = (closePrices[cnt - 1] - closePrices[0]) / closePrices[0] * 100;
      }
      else
      {
        currentValue = 0;
      }
    }
  }
}
